﻿using System;
using System.Collections;
using System;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using Microsoft.ApplicationBlocks.Data;

namespace AnyOffice.general.attend.manage
{
    public partial class export_sxb : ModulePage
    {
   
        protected void Page_Load(object sender, EventArgs e)
        {
            DateTime time = (Request.QueryString["date1"] != null) ? Convert.ToDateTime(Request.QueryString["date1"].ToString()) : DateTime.Now;
            DateTime time2 = (Request.QueryString["date2"] != null) ? Convert.ToDateTime(Request.QueryString["date2"].ToString()) : DateTime.Now;
            int num = 0;
            if ((Request.QueryString["dept_id"] != null) && (Request.QueryString["dept_id"].ToString() != string.Empty))
            {
                num = Convert.ToInt32(Request.QueryString["dept_id"].ToString());
            }
            int num2 = 0;
            if ((Request.QueryString["duty_type_id"] != null) && (Request.QueryString["duty_type_id"].ToString() != string.Empty))
            {
                num2 = Convert.ToInt32(Request.QueryString["duty_type_id"].ToString());
            }
            TimeSpan span = new TimeSpan(time.Ticks);
            TimeSpan ts = new TimeSpan(time2.Ticks);
            int num3 = span.Subtract(ts).Duration().Days + 1;
            string commandText = "SELECT [Users].[UserId],[Users].[UserName],[Department].[DeptName] FROM [dbo].[Users],[dbo].[Department] WHERE [Users].[DeptId] <> -1 AND [Users].[DeptId] = [Department].[DeptId] ";
            if (num != 0)
            {
                commandText = commandText + " AND [Users].[DeptId] = " + num.ToString();
            }
            if (num2 != 0)
            {
                commandText = commandText + " AND [Users].[DutyType] = " + num2.ToString();
            }
            commandText = commandText + " ORDER BY [Department].[DeptNo] ASC, [Users].[UserNo] ASC";
            DataTable table = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, commandText).Tables[0];
            table.Columns.Add("AllNum", Type.GetType("System.Int32"));
            table.Columns.Add("LaterNum", Type.GetType("System.Int32"));
            table.Columns.Add("UpNum", Type.GetType("System.Int32"));
            table.Columns.Add("BeforeNum", Type.GetType("System.Int32"));
            table.Columns.Add("DownNum", Type.GetType("System.Int32"));
            int num4 = 30;
            int num5 = 10;
            int num6 = 10;
            int num7 = 60;
            AttendParaDao dao = new AttendParaDao();
            dao.GetPara();
            num4 = dao.DutyIntervalBefore1;
            num5 = dao.DutyIntervalAfter1;
            num6 = dao.DutyIntervalBefore2;
            num7 = dao.DutyIntervalAfter2;
            if (table.Rows.Count > 0)
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    int num9;
                    DateTime time4;
                    DateTime time5;
                    DateTime time6;
                    string userId = table.Rows[i]["UserId"].ToString();
                    DataTable userConfig = new AttendConfigDao().GetUserConfig(base.Conn, userId);
                    string str3 = (userConfig.Rows[0]["General"] != DBNull.Value) ? userConfig.Rows[0]["General"].ToString() : "";
                    if (str3 != string.Empty)
                    {
                        str3 = "," + str3 + ",";
                    }
                    DataTable table3 = new DataTable();
                    table3.Columns.Add("DutyType", Type.GetType("System.Int32"));
                    table3.Columns.Add("DutyTime", Type.GetType("System.DateTime"));
                    table3.Columns.Add("BeginTime", Type.GetType("System.DateTime"));
                    table3.Columns.Add("EndTime", Type.GetType("System.DateTime"));
                    if (userConfig.Rows[0]["DutyTime1"] != DBNull.Value)
                    {
                        num9 = Convert.ToInt32(userConfig.Rows[0]["DutyType1"].ToString());
                        time4 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + userConfig.Rows[0]["DutyTime1"].ToString());
                        if (num9 == 1)
                        {
                            time5 = time4.AddMinutes((double)-num4);
                            time6 = time4.AddMinutes((double)num5);
                        }
                        else
                        {
                            time5 = time4.AddMinutes((double)-num6);
                            time6 = time4.AddMinutes((double)num7);
                        }
                        table3.Rows.Add(new object[] { num9, time4, time5, time6 });
                    }
                    if (userConfig.Rows[0]["DutyTime2"] != DBNull.Value)
                    {
                        num9 = Convert.ToInt32(userConfig.Rows[0]["DutyType2"].ToString());
                        time4 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + userConfig.Rows[0]["DutyTime2"].ToString());
                        if (num9 == 1)
                        {
                            time5 = time4.AddMinutes((double)-num4);
                            time6 = time4.AddMinutes((double)num5);
                        }
                        else
                        {
                            time5 = time4.AddMinutes((double)-num6);
                            time6 = time4.AddMinutes((double)num7);
                        }
                        table3.Rows.Add(new object[] { num9, time4, time5, time6 });
                    }
                    if (userConfig.Rows[0]["DutyTime3"] != DBNull.Value)
                    {
                        num9 = Convert.ToInt32(userConfig.Rows[0]["DutyType3"].ToString());
                        time4 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + userConfig.Rows[0]["DutyTime3"].ToString());
                        if (num9 == 1)
                        {
                            time5 = time4.AddMinutes((double)-num4);
                            time6 = time4.AddMinutes((double)num5);
                        }
                        else
                        {
                            time5 = time4.AddMinutes((double)-num6);
                            time6 = time4.AddMinutes((double)num7);
                        }
                        table3.Rows.Add(new object[] { num9, time4, time5, time6 });
                    }
                    if (userConfig.Rows[0]["DutyTime4"] != DBNull.Value)
                    {
                        num9 = Convert.ToInt32(userConfig.Rows[0]["DutyType4"].ToString());
                        time4 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + userConfig.Rows[0]["DutyTime4"].ToString());
                        if (num9 == 1)
                        {
                            time5 = time4.AddMinutes((double)-num4);
                            time6 = time4.AddMinutes((double)num5);
                        }
                        else
                        {
                            time5 = time4.AddMinutes((double)-num6);
                            time6 = time4.AddMinutes((double)num7);
                        }
                        table3.Rows.Add(new object[] { num9, time4, time5, time6 });
                    }
                    if (userConfig.Rows[0]["DutyTime5"] != DBNull.Value)
                    {
                        num9 = Convert.ToInt32(userConfig.Rows[0]["DutyType5"].ToString());
                        time4 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + userConfig.Rows[0]["DutyTime5"].ToString());
                        if (num9 == 1)
                        {
                            time5 = time4.AddMinutes((double)-num4);
                            time6 = time4.AddMinutes((double)num5);
                        }
                        else
                        {
                            time5 = time4.AddMinutes((double)-num6);
                            time6 = time4.AddMinutes((double)num7);
                        }
                        table3.Rows.Add(new object[] { num9, time4, time5, time6 });
                    }
                    if (userConfig.Rows[0]["DutyTime6"] != DBNull.Value)
                    {
                        num9 = Convert.ToInt32(userConfig.Rows[0]["DutyType6"].ToString());
                        time4 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + userConfig.Rows[0]["DutyTime6"].ToString());
                        if (num9 == 1)
                        {
                            time5 = time4.AddMinutes((double)-num4);
                            time6 = time4.AddMinutes((double)num5);
                        }
                        else
                        {
                            time5 = time4.AddMinutes((double)-num6);
                            time6 = time4.AddMinutes((double)num7);
                        }
                        table3.Rows.Add(new object[] { num9, time4, time5, time6 });
                    }
                    int num10 = 0;
                    int num11 = 0;
                    int num12 = 0;
                    int num13 = 0;
                    int num14 = 0;
                    for (int j = 0; j < num3; j++)
                    {
                        bool flag;
                        bool flag2;
                        int dayOfWeek = (int)time.AddDays((double)j).DayOfWeek;
                        if (str3.IndexOf("," + dayOfWeek.ToString() + ",") < 0)
                        {
                            flag = true;
                        }
                        else
                        {
                            flag = false;
                        }
                        commandText = "SELECT [HolidayId] FROM [dbo].[Attend_Holiday] WHERE [BeginDate] <='" + time.AddDays((double)j).ToString("yyyy-MM-dd") + "' AND '" + time.AddDays((double)j).ToString("yyyy-MM-dd") + "' <= [EndDate]";
                        if (SqlHelper.ExecuteScalar(base.Conn, CommandType.Text, commandText) != null)
                        {
                            flag2 = true;
                            flag = false;
                        }
                        else
                        {
                            flag2 = false;
                        }
                        commandText = "SELECT DutyId,RegisterType,RegisterTime,RegisterFlag,Remark FROM [dbo].[Attend_Duty] WHERE [UserId] = '" + userId + "' AND [RegisterTime] >='" + time.AddDays((double)j).ToString("yyyy-MM-dd") + " 00:00:00' AND [RegisterTime] <= '" + time.AddDays((double)j).ToString("yyyy-MM-dd") + " 23:59:59'";
                        DataTable table4 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, commandText).Tables[0];
                        for (int k = 0; k < table3.Rows.Count; k++)
                        {
                            DataRow[] rowArray = table4.Select("RegisterType = " + table3.Rows[k]["DutyType"].ToString() + " AND RegisterTime >='" + time.AddDays((double)j).ToString("yyyy-MM-dd") + " " + Convert.ToDateTime(table3.Rows[k]["BeginTime"].ToString()).ToString("HH:mm:ss") + "' AND RegisterTime <='" + time.AddDays((double)j).ToString("yyyy-MM-dd") + " " + Convert.ToDateTime(table3.Rows[k]["EndTime"].ToString()).ToString("HH:mm:ss") + "'");
                            if (rowArray.Length > 0)
                            {
                                if (Convert.ToInt32(rowArray[0]["RegisterFlag"].ToString()) == 1)
                                {
                                    flag = false;
                                    num11++;
                                }
                                else if (Convert.ToInt32(rowArray[0]["RegisterFlag"].ToString()) == 2)
                                {
                                    flag = false;
                                    num13++;
                                }
                            }
                            else if ((str3.IndexOf("," + dayOfWeek.ToString() + ",") < 0) && !flag2)
                            {
                                commandText = "SELECT [OutId] FROM [dbo].[Attend_Out] WHERE [Allow] = 1 AND [UserId] ='" + userId + "' AND [OutTime1] <='" + time.AddDays((double)j).ToString("yyyy-MM-dd") + " " + Convert.ToDateTime(table3.Rows[k]["DutyTime"].ToString()).ToString("HH:mm:ss") + "' AND [OutTime2]>='" + time.AddDays((double)j).ToString("yyyy-MM-dd") + " " + Convert.ToDateTime(table3.Rows[k]["DutyTime"].ToString()).ToString("HH:mm:ss") + "'";
                                DataTable table5 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, commandText).Tables[0];
                                if (table5.Rows.Count == 0)
                                {
                                    commandText = "SELECT [LeaveId] FROM [dbo].[Attend_Leave] WHERE [Allow] = 1 AND [UserId] ='" + userId + "' AND [LeaveDate1] <='" + time.AddDays((double)j).ToString("yyyy-MM-dd") + " " + Convert.ToDateTime(table3.Rows[k]["DutyTime"].ToString()).ToString("HH:mm:ss") + "' AND [LeaveDate2]>='" + time.AddDays((double)j).ToString("yyyy-MM-dd") + " " + Convert.ToDateTime(table3.Rows[k]["DutyTime"].ToString()).ToString("HH:mm:ss") + "'";
                                    DataTable table6 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, commandText).Tables[0];
                                    if (table6.Rows.Count == 0)
                                    {
                                        commandText = "SELECT [EvectionId] FROM [dbo].[Attend_Evection] WHERE [Allow] = 1 AND [UserId] ='" + userId + "' AND [EvectionDate1] <='" + time.AddDays((double)j).ToString("yyyy-MM-dd") + " " + Convert.ToDateTime(table3.Rows[k]["DutyTime"].ToString()).ToString("HH:mm:ss") + "' AND [EvectionDate1]>='" + time.AddDays((double)j).ToString("yyyy-MM-dd") + " " + Convert.ToDateTime(table3.Rows[k]["DutyTime"].ToString()).ToString("HH:mm:ss") + "'";
                                        DataTable table7 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, commandText).Tables[0];
                                        if (table7.Rows.Count == 0)
                                        {
                                            if (Convert.ToInt32(table3.Rows[k]["DutyType"].ToString()) == 1)
                                            {
                                                flag = false;
                                                num12++;
                                            }
                                            else
                                            {
                                                flag = false;
                                                num14++;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        if (flag)
                        {
                            num10++;
                        }
                    }
                    if (num10 != 0)
                    {
                        table.Rows[i]["AllNum"] = num10;
                    }
                    if (num11 != 0)
                    {
                        table.Rows[i]["LaterNum"] = num11;
                    }
                    if (num12 != 0)
                    {
                        table.Rows[i]["UpNum"] = num12;
                    }
                    if (num13 != 0)
                    {
                        table.Rows[i]["BeforeNum"] = num13;
                    }
                    if (num14 != 0)
                    {
                        table.Rows[i]["DownNum"] = num14;
                    }
                }
                Response.Clear();
                Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("上下班登记数据.csv"));
                Response.Write("部门,姓名,全勤(天),迟到,上班未登记,早退,下班未登记\n");
                if (table.Rows.Count > 0)
                {
                    for (int m = 0; m < table.Rows.Count; m++)
                    {
                        Response.Write(table.Rows[m]["DeptName"].ToString() + ",");
                        Response.Write(table.Rows[m]["UserName"].ToString() + ",");
                        Response.Write(table.Rows[m]["AllNum"].ToString() + ",");
                        Response.Write(table.Rows[m]["LaterNum"].ToString() + ",");
                        Response.Write(table.Rows[m]["UpNum"].ToString() + ",");
                        Response.Write(table.Rows[m]["BeforeNum"].ToString() + ",");
                        Response.Write(table.Rows[m]["DownNum"].ToString() + ",");
                        Response.Write("\n");
                    }
                }
            }
        } 

    }
}
